package com.example.administrator.myapplication;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.example.administrator.myapplication.storeage.JSWrapper;
import com.example.administrator.myapplication.storeage.Person;

import java.util.Vector;
import java.util.logging.Logger;

public class WebkitActivity extends AppCompatActivity {


    private Logger logger = Logger.getLogger("WebkitActivity");
    private Activity activity = this;

    private AlertDialog alertDialog;
    private ProgressDialog progressDialog;

    private Vector<Person> phonebook = new Vector<Person>();


    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_webkit);

        initLocalWeb();
        //initWebView();



    }

    @SuppressLint("JavascriptInterface")
    private void initLocalWeb() {
        webView = (WebView) findViewById(R.id.webview);
        initContacts();
        webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface(this,"phonebook");
        webView.loadUrl("file:///android_asset/phonebook.html");

    }

    public JSWrapper getContacts() {
        Person[] a = new Person[this.phonebook.size()];
        a = this.phonebook.toArray(a);
        return new JSWrapper(a);
    }

    public void debugout(String info) {
        logger.info("debug : " +info);
        System.out.println(info);
    }

    //初始化联系人
    private void initContacts() {

        Person p = new Person();
        p.setName("Perter");
        p.setName("123456");

        phonebook.add(p);


        Person a  = new Person();
        p.setName("Tom");
        p.setName("888888");
        phonebook.add(a);

    }

    private void initWebView() {

        progressDialog = ProgressDialog.show(WebkitActivity.this,null,"正在加载网页，请稍后");
        webView = (WebView) findViewById(R.id.webview);
        //设置Web视图
        webView.setWebViewClient(new HelloWebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("https://gold.xitu.io/");

        alertDialog = new AlertDialog.Builder(WebkitActivity.this).create();


    }

    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            logger.info("==============url : " + url);
            progressDialog = ProgressDialog.show(WebkitActivity.this,null,"正在加载网页，请稍后");
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if(progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(WebkitActivity.this,"网页加载出错",Toast.LENGTH_SHORT);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return false;
    }

}
